package com.fyzk.activity.user.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.net.http.NetWork;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LogUtil;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Pattern;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * 修改密码
 * @author fxs
 *
 */
public class ChangePasswordActivity extends BaseActivity implements onTopTitleClickListener {
	
	private static final String TAG = "ChangePasswordActivity";
	
	/**
	 * 用户名
	 */
	private String login_name;
	private TopTitleView topView;
	private TextView center_loginName;
	
	private EditText center_oldPwd;
	private EditText center_newPwd;
	private EditText center_sureNewPwd;
	
	private TextView sure;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_changepwd);
		NetWork.netWork();
		
		login_name = getIntent().getStringExtra(IntentParamUtils.USERNAME);
		
		initView();
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	};
	
	protected void onResume() {
		super.onResume();
	};

	private void requestChangePwd() {
		try {
			JSONObject request_json = new JSONObject();
			request_json.put("oldPassword", center_oldPwd.getText().toString());
			request_json.put("newPassword", center_newPwd.getText().toString());
			httpPostJson(CommandUtil.USER_CHANGEPSW, Commands.CHANGEPASSWORD, request_json, false);
		} catch (JSONException e) {
			LogUtil.d("参数异常");
		}
	}

	@Override
	protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
		super.onSuccess(requestCode, resultBean, result);
		AppToast.getToast().show("修改成功，请重新登录");
		clearPSWEditText();
		AppLotteryInfos.clear();
		ViewCallBack.getInstance().countDownObjs.clear();
		ActivityTaskManager.getInstance().closeAllActivity();
		MySharedPreferences.getInstance().setLoginStatus(false);
		Intent intent = new Intent(getContext(), LoginActivity.class);
		startActivity(intent);
	}

	@Override
	protected void onFailure(int requestCode, String message) {
		super.onFailure(requestCode, message);
	}

	/**
	 * 控件初始化
	 */
	private void initView(){
		//========	顶部  top	============
		topView = (TopTitleView) findViewById(R.id.changepwd_topview);
		topView.setRightVisible(View.INVISIBLE);
		topView.setCenterText(getString(R.string.user_zhu_menu_xiugaimima));
		topView.setOnTitleClickListener(this);
		
		//========	中间  center	=========
		center_loginName = (TextView)findViewById(R.id.changepwd_username);
		center_loginName.setText(login_name);
		center_oldPwd = (EditText)findViewById(R.id.changepwd_center_et_oldmima);
		center_newPwd = (EditText)findViewById(R.id.changepwd_center_et_newmima);
		center_sureNewPwd = (EditText)findViewById(R.id.changepwd_center_et_surenewmima);
		
		sure = (TextView)findViewById(R.id.changepwd_sure);
		
		/***  事件	***/
		
		sure.setOnClickListener(new InforOnClick());
		
	}

	
	/**
	 * 修改完密码后清除edittext输入框
	 */
	private void clearPSWEditText(){
		center_oldPwd.setText("");
		center_newPwd.setText("");
		center_sureNewPwd.setText("");
	}
	
	/**
	 * 非空验证
	 */
	private boolean isNull(EditText editText){
		if (editText.getText() == null || editText.getText().toString().equals("")) {
			editText.requestFocus();
			return true;
		}
		
		return false;
	}
	
	/**
	 * 新密码验证
	 */
	private boolean yanZheng_pwd(){
		if (isNull(center_oldPwd)) {
			AppToast.getToast().show("原密码不能为空！");
			return false;
		}
		if (isNull(center_newPwd)) {
			AppToast.getToast().show("新密码不能为空！");
			return false;
		}
		if (!Pattern.matches("[a-zA-Z0-9]{6,15}$", center_newPwd.getText().toString())) {
			AppToast.getToast().show("密码由6~15位字母、数字组成！");
			center_newPwd.requestFocus();
			center_newPwd.setSelectAllOnFocus(true);
			return false;
		}
		if (!center_newPwd.getText().toString().equalsIgnoreCase(center_sureNewPwd.getText().toString())) {
			AppToast.getToast().show("两次输入新密码不符！");
			center_sureNewPwd.requestFocus();
			center_sureNewPwd.setSelectAllOnFocus(true);
			return false;
		}
		
		return true;
	}
	
	
	/**
	 * 单击事件
	 * @author Administrator
	 *
	 */
	class InforOnClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.changepwd_sure:
				if (yanZheng_pwd()) {
					requestChangePwd();
				}
				break;
			default:
				break;
			}
		}
		
	}


	@Override
	public void onLeftClick() {
		// TODO Auto-generated method stub
		onBackPressed();
	}

	@Override
	public void onCenterClick() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRightClick() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRight2Click() {
		// TODO Auto-generated method stub
		
	}
	

}
